package com.huobao.yunying.adapters;

import java.util.List;

import com.huobao.yunying.activitys.muyingquan.FristHostTiezi;
import com.huobao.yunying.utils.ImageUtils;
import com.huobao.yunying.utils.ImageUtils.OnImageLoadListener;
import com.huobao.yunying.views.RoundImageView;
import com.huobao.zhangying.R;
import com.xrz.sxm.base.utils.ScreenUtil;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
import joanzapata.android.BaseAdapterHelper;
import joanzapata.android.QuickAdapter;

public class Adapter_Host_Tiezi extends QuickAdapter<FristHostTiezi> {

	public Adapter_Host_Tiezi(Context context) {
		super(context, R.layout.item_host_tiezi);
	}

	@Override
	protected void convert(BaseAdapterHelper helper, FristHostTiezi data) {
		TextView tv_jing = helper.getView(R.id.tv_jing);
		TextView tv_re = helper.getView(R.id.tv_re);
		TextView tv_ding = helper.getView(R.id.tv_ding);
		TextView tv_title = helper.getView(R.id.tv_title);
		TextView tv_storename = helper.getView(R.id.tv_storename);
		TextView tv_content = helper.getView(R.id.tv_content);
		ImageView img_1 = helper.getView(R.id.img_1);
		ImageView img_2 = helper.getView(R.id.img_2);
		ImageView img_3 = helper.getView(R.id.img_3);
		TextView tv_quanzi_name = helper.getView(R.id.tv_quanzi_name);
		TextView tv_time = helper.getView(R.id.tv_time);
		TextView tv_pinglun = helper.getView(R.id.tv_pinglun);
		RoundImageView img_photo = helper.getView(R.id.img_userphoto);
		img_photo.setRectAdius(90);
		LinearLayout llyt_imgs = helper.getView(R.id.llyt_10);
		tv_jing.setVisibility(data.getIsbetter().equals("1") ? View.VISIBLE : View.GONE);
		tv_re.setVisibility(data.getIshot().equals("1") ? View.VISIBLE : View.GONE);
		tv_ding.setVisibility(data.getIsfirst().equals("1") ? View.VISIBLE : View.GONE);
		tv_title.setText(data.getTitle());
		tv_storename.setText(data.getUser_name());
		tv_content.setText(data.getContent());
		tv_quanzi_name.setText(data.getGname());
		tv_time.setText(data.getCreate_time());
		tv_pinglun.setText(data.getComments());
		
		ImageUtils.loadimg(img_photo, data.getPortrait());

		List<String> images = data.getImages();
		if (images == null || images.size() == 0) {
			llyt_imgs.setVisibility(View.GONE);
		} else {
			llyt_imgs.setVisibility(View.VISIBLE);
			switch (images.size()) {
			case 1:
				img_1.setVisibility(View.VISIBLE);
				img_2.setVisibility(View.INVISIBLE);
				img_3.setVisibility(View.INVISIBLE);
				break;
			case 2:
				img_1.setVisibility(View.VISIBLE);
				img_2.setVisibility(View.VISIBLE);
				img_3.setVisibility(View.INVISIBLE);
				loadImg(img_2, images.get(1));
				break;
			case 3:
			default:
				img_1.setVisibility(View.VISIBLE);
				img_2.setVisibility(View.VISIBLE);
				img_3.setVisibility(View.VISIBLE);
				loadImg(img_2, images.get(1));
				loadImg(img_3, images.get(2));
				
				break;
			}
			loadImg(img_1, images.get(0));
		}

	}

	private void loadImg(final ImageView image,String url){
		ImageUtils.loadimg(image, url, new OnImageLoadListener() {
			
			@Override
			public void onLoadSuccess(Drawable drawable) {
				int oldWidth = drawable.getIntrinsicWidth();
				int oldHight = drawable.getIntrinsicHeight();
				int newWidth = ScreenUtil.getInstant().getDisWidth(context) / 3 - 10;
				int newHight = newWidth * oldHight / oldWidth;
				LayoutParams layoutParams = (LayoutParams) image.getLayoutParams();
				layoutParams.width = newWidth;
				layoutParams.height = newHight;
				image.setLayoutParams(layoutParams);
			}
		});
	}
}
